package com.cqupt.service.impl;

import com.cqupt.common.log.aop.LogAopAction;
import com.cqupt.mapper.UserMapper;
import com.cqupt.pojo.User;
import com.cqupt.pojo.UserExample;
import com.cqupt.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取当前日志对象
     */
    private static final Logger logger = Logger.getLogger(UserServiceImpl.class);

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> login(User user) {
//        select username, role from user where username =#{username,jdbcType=VARCHAR} and password =#{password,jdbcType=VARCHAR}
        Map<String, Object> loginResult = new HashMap<>();
        // 1为成功
        // 2为用户名不存在
        // 3为密码错误
        // 0为用户名或密码错误
        int flag = 0;
        List<Map<String, Object>> loginResultList = userMapper.login(user);
        Map<String, Object> np = userMapper.getNameAndPwd(user.getUsername());
        if(!loginResultList.isEmpty()){
//           Map<String,Object> loginResultTmp = loginResultList.get(0);
//           for(String t:loginResultTmp.keySet()){
//               loginResult.put(t,loginResultTmp.get(t));
//           }
//            System.out.println(loginResult);

            loginResult = loginResultList.get(0);
            flag = 1;
            LogAopAction.getUser(new User(user.getUsername(),user.getPassword(),flag));
            logger.info("本方法被执行");
            return loginResult;
        }
        /**
         * 如果用户为空，则返回用户不存在
         */
        if (np==null)
            flag = 2;
        /**
         * 如果用户不为空，但是密码不正确，返回密码不增强
         */
        else if (np!=null && np.get("password")!=user.getPassword())
            flag = 3;
        /**
         * 否则返回用户名或者密码不正确
         */
        else
            flag =0;
        LogAopAction.getUser(new User(user.getUsername(),user.getPassword(),flag));
        logger.info("本方法被执行");
        return loginResult;
    }

    /**
     * 注销
     * @param username
     * @return
     */
    @Override
    public Boolean logout(String username) {
        /**
         * 使用日志切片
         */
        LogAopAction.getUsername(username);
        return true;
    }


    /**
     * 新增用户
     * @param user
     * @return
     */
    @Override
    public Boolean addUser(User user) {
        User existUser = userMapper.selectByPrimaryKey(user.getUserId());
        if(existUser == null) {
            int count = userMapper.insert(user);
            if(count > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @Override
    public Boolean deleteUser(User user) {
        int count = userMapper.deleteByPrimaryKey(user.getUserId());
        if(count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @Override
    public Boolean changeUser(User user) {
        int count = userMapper.updateByPrimaryKeySelective(user);
        if(count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过分页获取用户
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<User> getUserByPageNumber(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<User> userList = userMapper.selectByExample(new UserExample());
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    /**
     * 查询用户
     * @param pageNumber
     * @param pageSize
     * @param user
     * @return
     */
    @Override
    public PageInfo<User> searchUser(int pageNumber, int pageSize, User user) {
        PageHelper.startPage(pageNumber, pageSize);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        criteria.andUserIdEqualTo(user.getUserId());
        criteria.andUserOrganizationEqualTo(user.getUserOrganization());
        criteria.andRoleEqualTo(user.getRole());
        criteria.andRealNameEqualTo(user.getRealName());
        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    /**
     * 通过用户id查询用户
     * @param userId
     * @return
     */
    @Override
    public User getUserByUserId(String userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    /**
     * 获取用户日志状态
     * @param userId
     * @return
     */
    @Override
    public String getUserLogStatus(String userId) {
        String status = userMapper.selectStatusByPrimaryKey(userId);
        return status;
    }

    /**
     * 设置用户状态
     * @param userId
     * @param status
     * @return
     */
    @Override
    public int setUserLogStatus(String userId, String status) {
        int flag = userMapper.setUserStatus(userId,status);
        return flag;
    }

}
